# -*- coding:utf-8 -*-
# @Time: 2020/9/8 11:15
# @Author: gupeng
# @File: counter.py
# @Desc: 计数器--hincrby测试

class Counter:
    def __init__(self, client, hash_key, counter_name):
        self.client = client
        self.hash_key = hash_key
        self.counter_name = counter_name

    def increase(self, n=1):
        """
        将计数器的值加上n，然后返回计数器当前的值
        :param n:需要加上的值，默认1
        :return:当前值
        """
        return self.client.hincrby(self.hash_key, self.counter_name, n)

    def decrease(self, n=1):
        """
        将计数器的值减去n，然后返回计数器当前的值
        :param n: 需要减去的值，默认1
        :return: 当前值
        """
        return self.client.hincrby(self.hash_key, self.counter_name, -n)

    def get(self):
        """
        返回计数器的当前值
        :return: 当前值
        """
        return self.client.hget(self.hash_key, self.counter_name)

    def reset(self):
        """
        将计数器的值重置为0
        :return: None
        """
        self.client.hset(self.hash_key, self.counter_name, 0)
